package com.beerandjesus.calc;

import com.beerandjesus.calc.parse.TrackAndTime;
import com.beerandjesus.calc.parse.TracksAndTimes;

import java.util.ArrayList;
import java.util.List;
import java.util.Calendar;
import java.text.SimpleDateFormat;

/**
 * Date: Aug 2, 2009
 * Time: 6:00:12 PM
 */
public class AbstractTrackSplitter {
    private List<String> trackNames = null;
    private List<String> stops = null;
    private Calendar calendar = Calendar.getInstance();
    private SimpleDateFormat format = new SimpleDateFormat("H'h' m:ss");
    private String both;
    private TracksAndTimes tracks = new TracksAndTimes();
    private boolean debug = false;

    public void setTrackNames(String tracks) {
        if (tracks == null) return;
        this.trackNames = new ArrayList<String>();

        String[] names = tracks.split("\n");
        for (String name : names) {
            trackNames.add(name.trim());
        }
    }

    public void setBoth(String both) {
        this.both = both;
        String[] lines = both.split("\n");
        for (String line : lines) {
            TrackAndTime trackAndTime = new TrackAndTime(line, TrackAndTime.InputFormat.ENDS_WITH_TIME);
            tracks.add(trackAndTime);
        }
    }

    public List<String> getTrackNames() {
        return trackNames;
    }

    public void setTrackTimes(String times) {
        if (times == null) return;
        stops = new ArrayList<String>();
        String[] nums = times.split("\n");
        calendar.clear();
        calendar.set(Calendar.HOUR, 0);
        for (String num : nums) {
            String[] minsAndSecs = num.split(":");
            String mins = minsAndSecs[0];
            String secs = minsAndSecs[1];
            if (mins == null || mins.equals("")) mins = "0";
            calendar.add(Calendar.MINUTE, Integer.parseInt(mins.trim()));
            calendar.add(Calendar.SECOND, Integer.parseInt(secs.trim()));
            stops.add(format.format(calendar.getTime()));
        }


    }

    public String getBoth() {
        return both;
    }

    public List<String> getStops() {
        return stops;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    public boolean isDebug() {
        return debug;
    }
    public TracksAndTimes getTracks() {
        return tracks;
    }

    public void setTracks(TracksAndTimes tracks) {
        this.tracks = tracks;
    }

    public Calendar getCalendar() {
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }
}
